<?php

class AttachmentController extends Controller
{
	public function actionAdd()
	{
		$this->render('add');
	}

	public function actionDelete()
	{
		$this->render('delete');
	}

	public function actionList($id)
	{
		$this->layout='//layouts/blank';
		$project = Project::model()->findByPk($id);
		$role=Yii::app()->session->get('USERTYPE');
		$this->render('list',array(
				'usertype'=>$role,
				'project'=>$project,
			));
	}

	public function actionView()
	{
		$this->render('view');
	}
	
	public function actionLoadAttachmentType()
	{
		$model = DocumentType::model()->findAll('doctypeID >=7');
		foreach($model as $item){
			$data[]=array('id'=>$item->doctypeID,'type'=>$item->type);
		}
		echo json_encode($data);
	}
	
	public function actionUpload()
	{
		$model = new Document();
		$model->unsetAttributes();
		$model->projectID=$_POST['projectID'];
		$model->typeID=$_POST['attachTypeID'];
		$model->docName=$_POST['attachTypeName'];
		$model->save();
		$tmp_file_suffix=explode("/",$_FILES['fileAttachUpload']['type']);
		$tmp_file_name=$model->documentID."-".date("YYmmddHHiiss").'.'.$tmp_file_suffix[1];
		$model->fileName=$tmp_file_name;
		$path = Yii::app()->basePath.'/../document/'.$tmp_file_name;
		//print_r($model);
		//exit;
		if (copy($_FILES['fileAttachUpload']['tmp_name'], $path)){
		//$this->redirect(array('list','id'=>$model->projectID));
		$model->save();
		echo "<script>parent.showAttachment(".$_POST['projectID'].");</script>";
		}
	}
}